﻿
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Http;
using CRM.Api.Filters;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using CRM.Utility;

namespace CRM.Api.Controllers
{
    /// <summary>
    /// 所有ApiController基类
    /// </summary>
    [APIExceptionFilter]
    //[APIAuthorizationFilter]
    [ActionCORS]
    public class BaseApiController : ApiController
    {

        /// <summary>
        /// 当前登陆人
        /// </summary>
        protected long CurrentUserID
        {
            get
            {
                return EConvert.ToLong(ActionContext.RequestContext.Principal.Identity.Name);
            }
        }

        /// <summary>
        /// 默认时间
        /// </summary>
        protected DateTime DefultTime
        {
            get
            {
                return Convert.ToDateTime("1900-01-01");
            }
        }
        
        /// <summary>
        /// 将对象转换成JSON
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        protected string SerializeObject(object value)
        {
            IsoDateTimeConverter timeFormat = new IsoDateTimeConverter
            {
                DateTimeFormat = "yyyy-MM-dd HH:mm:ss"
            };
            return JsonConvert.SerializeObject(value, Formatting.Indented, timeFormat);
        }

        /// <summary>
        /// 将JSON字符串转换成对象
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="value"></param>
        /// <returns></returns>
        protected T DeserializeObject<T>(string value)
        {
            return JsonConvert.DeserializeObject<T>(value);
        }

        /// <summary>
        /// 获取默认路径
        /// </summary>
        /// <param name="str"></param>
        /// <param name="src"></param>
        /// <returns></returns>
        protected string GetDefaultString(string str, string src)
        {
            if (!string.IsNullOrEmpty(str))
            {
                if (str.StartsWith("http"))
                {
                    return str;
                }
                else
                {
                    return src + str.Replace("\"", "");
                }
            }
            return "";
        }

    }
}
